package com.xjtu.conan.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.conan.entity.ArticleImagesInfo;
import com.xjtu.conan.entity.NewsImagesInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.exception.MyException;
import com.xjtu.conan.mapper.ArticleImagesInfoMapper;
import com.xjtu.conan.service.ArticleImagesInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjtu.conan.utils.DocumentUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 话题图片表 服务实现类
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@Slf4j
@Service
public class ArticleImagesInfoServiceImpl extends ServiceImpl<ArticleImagesInfoMapper, ArticleImagesInfo> implements ArticleImagesInfoService {

    @Autowired
    private ArticleImagesInfoMapper articleImagesInfoMapper;

    @Override
    public List<ArticleImagesInfo> getAllArticleImageInfo(int current, int size) {
        Page<ArticleImagesInfo> page = new Page<>(current, size);
        articleImagesInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public ArticleImagesInfo getArticleImageByid(Long aiid) {
        ArticleImagesInfo articleImages = articleImagesInfoMapper.selectById(aiid);
        if (articleImages == null) {
            throw new MyException(500, "不存在该新闻图片！");
        }
        return articleImages;
    }

    @Override
    public int addArticleImages(MultipartFile[] images, Long nid, UserInfo loginUser) throws IOException {
        int result = 0;
//        Long uid = loginUser.getUid();
        Long uid = 1384057153556135937L;
        try {
            if (images != null && images.length > 0) {
                for (MultipartFile file : images) {
                    String fileName = file.getOriginalFilename();
                    assert fileName != null;
                    ArticleImagesInfo articleImagesInfo = new ArticleImagesInfo();
                    if (fileName.endsWith("jpg") || fileName.endsWith("png") || fileName.endsWith("jpeg")) {
                        Map<String, String> temps = DocumentUtil.saveDocument(fileName);
                        String savePath = temps.get("filePath");
                        String saveUrl = temps.get("fileUrl");
                        articleImagesInfo.setAiPath(savePath)
                                .setAiUrl(saveUrl)
                                .setAiid(nid)
                                .setAiName(fileName)
                                .setUpdateUserId(uid);
                        file.transferTo(new File(savePath));
                        result += articleImagesInfoMapper.insert(articleImagesInfo);
                    } else {
                        throw new MyException(400, "文件" + fileName + "格式错误！请上传正确的图片文件！");
                    }

                }
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new MyException(500, "添加话题图片失败！");
        }
        return result;
    }

    @Override
    public List<ArticleImagesInfo> getArticleImagesByaid(Long aid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("article_id", aid);
        return articleImagesInfoMapper.selectByMap(map);
    }

    @Override
    public int deleteArticleImages(Long aiid) {
        try {
            String originalFile = this.getArticleImageByid(aiid).getAiPath();
            if (articleImagesInfoMapper.deleteById(aiid) == 1) {
                File file = new File(originalFile);
                if (file.exists()) {
                    boolean delete = file.delete();
                    log.info("删除原话题图片成功！");
                }
            }
            return 1;
        } catch (Exception e) {
            throw new MyException(500, "删除原话题图片失败！");
        }
    }

    @Override
    public int getTotal() {
        return articleImagesInfoMapper.selectCount(null);
    }
}
